/**
 * jQuery Shake plugin
 * Created by Igor Ramadas for http://counting.to
 */
(function($)
{
    $.fn.shake = function(c)
    {
        // Set options and defaults.
        // The higher the "speed" value, the faster the objects will shake, from 1 to 10.
        // The higher the "duration" value, the longer it will stay shaking.
        // The higher the "strength", well, the stronger it will shake.
        var options = $.extend({
            speed: 8,
            duration: 2000,
            strength: 8
        }, c);

        return this.each(function()
        {
            // Cache the current element.
            var el = $(this);

            // The actual function to shake the whole thing.
            var shaker = function()
            {
                var top = Math.floor(Math.random() * options.strength) - ((options.strength - 1) / 2),
                    left = Math.floor(Math.random() * options.strength) - ((options.strength - 1) / 2),
                    rotate = Math.floor(Math.random() * options.strength) - ((options.strength - 1) / 2);

                // Set the random CSS.
                el.css({
                    position: "relative",
                    top: top + "px",
                    left: left + "px",
                    WebkitTransform: "rotate(" + rotate + "deg)"
                });
            };

            // Do the harlem shake!!!
            var doTheHarlemShake = function()
            {
                var shakerTimer = setInterval(shaker, 201 - 20 * options.speed);
                var stopVibration = function()
                {
                    clearInterval(shakerTimer);

                    // Reset the element CSS.
                    el.css({
                        position: "static",
                        WebkitTransform: "rotate(0deg)"
                    });
                };

                // Stop shaking after the specified duration.
                setTimeout(stopVibration, options.duration);
            };

            doTheHarlemShake();
        });
    };
})(jQuery);